package com.yst.shader.feature.shimmer

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.children
import androidx.core.view.setPadding
import androidx.fragment.app.Fragment
import com.yst.shader.R
import com.yst.shader.ext.hoverOnFocusInChildren
import kotlinx.android.synthetic.main.fragment_shader.*

/**
 * @author: xuyujian@ysten.com
 *  @date: 2020/8/14 13:55
 */
class ShaderSimpleFragment : Fragment(R.layout.fragment_shader) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.post {
            ll_test.hoverOnFocusInChildren()
            ll_test.children.forEachIndexed { index, view ->
                if (index == 1) return@forEachIndexed
                val v = (view as? ImageView) ?: return@forEachIndexed

                if (index == 2) {
                    v.setBackgroundResource(R.drawable.focus_v2)
                    v.setPadding(15)
                }

                v.setImageResource(R.drawable.test)
            }

        }
        view.viewTreeObserver.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
            println("focus change old:$oldFocus , new $newFocus")
            iv_place?.focusRoundChange(oldFocus, newFocus)
        }
        view.viewTreeObserver.addOnScrollChangedListener {
            iv_place?.requestNewFocus()
        }
    }

}